package com.ruoyi.disCustomer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.disCustomer.domain.bo.TCustomerDisRecordBo;
import com.ruoyi.disCustomer.domain.vo.TCustomerDisRecordVo;
import com.ruoyi.disCustomer.service.ITCustomerDisRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 客户分配记录
 *
 * @author ruoyi
 * @date 2023-08-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/disCustomer/customerDisRecord")
public class TCustomerDisRecordController extends BaseController {

    private final ITCustomerDisRecordService iTCustomerDisRecordService;

    /**
     * 查询客户分配记录列表
     */
    @SaCheckPermission("system:customerDisRecord:list")
    @GetMapping("/list")
    public TableDataInfo<TCustomerDisRecordVo> list(TCustomerDisRecordBo bo, PageQuery pageQuery) {
        return iTCustomerDisRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户分配记录列表
     */
    @SaCheckPermission("system:customerDisRecord:export")
    @Log(title = "客户分配记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TCustomerDisRecordBo bo, HttpServletResponse response) {
        List<TCustomerDisRecordVo> list = iTCustomerDisRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户分配记录", TCustomerDisRecordVo.class, response);
    }

    /**
     * 获取客户分配记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:customerDisRecord:query")
    @GetMapping("/{id}")
    public R<TCustomerDisRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(iTCustomerDisRecordService.queryById(id));
    }

    /**
     * 新增客户分配记录
     */
    @SaCheckPermission("system:customerDisRecord:add")
    @Log(title = "客户分配记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TCustomerDisRecordBo bo) {
        return toAjax(iTCustomerDisRecordService.insertByBo(bo));
    }

    /**
     * 修改客户分配记录
     */
    @SaCheckPermission("system:customerDisRecord:edit")
    @Log(title = "客户分配记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TCustomerDisRecordBo bo) {
        return toAjax(iTCustomerDisRecordService.updateByBo(bo));
    }

    /**
     * 删除客户分配记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:customerDisRecord:remove")
    @Log(title = "客户分配记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTCustomerDisRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
